package com.naizhuai.ui.task.multtype;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.task.TaskDetialActivity;
import com.naizhuai.util.MathUtils;
import com.naizhuai.util.ViewUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.drakeet.multitype.ItemViewBinder;

/**
 * email：525603977@qq.com
 * Fighting
 */
public class TaskItemViewBinder extends ItemViewBinder<TaskItem, TaskItemViewBinder.ViewHolder> {



    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.task_item, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull TaskItem taskItem) {
        final Context context = holder.itemView.getContext();
        ViewUtils.showImg(taskItem.managerInfo.thumb,holder.circleImageView);

        holder.taskname.setText(taskItem.taskName);
//        String completeCount = String.format(context.getText(R.string.mission_number_finished).toString(), taskItem.completeCount);
//        String unCompleteCount = String.format(context.getText(R.string.mission_number_unfinished).toString(), taskItem.unCompleteCount);

        holder.joinPerson.setText(context.getString(R.string.join_person_format,nullStr(taskItem.completeCount)));
        holder.taskFinished.setText(context.getString(R.string.change_action_format,nullStr(taskItem.unCompleteCount)));

        if (TaskItem.noSee.equals(taskItem.redPoint)) {
            holder.redMsg.setVisibility(View.VISIBLE);
        }
        //显示进度问题
        double totalTime = MathUtils.numberSubstrationReNUmber(taskItem.taskEndTime, taskItem.taskStartTime);
        double currentTime = MathUtils.numberSubstrationReNUmber(taskItem.taskEndTime, System.currentTimeMillis() / 1000 + "");
        holder.itemView.post(() -> {
            if (currentTime < 0) {
                ViewGroup.LayoutParams layoutParams = holder.realProgress.getLayoutParams();
                layoutParams.width = holder.totalProgress.getWidth();
                holder.realProgress.setLayoutParams(layoutParams);
            } else {
                double currentIndex = currentTime / totalTime;
                int showWidth = (int) (holder.totalProgress.getWidth() * currentIndex);
                ViewGroup.LayoutParams layoutParams = holder.realProgress.getLayoutParams();
                if (showWidth >= holder.totalProgress.getWidth()) {
                    showWidth = holder.totalProgress.getWidth();
                }
                layoutParams.width = showWidth;
                holder.realProgress.setLayoutParams(layoutParams);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击之后 进入任务详情页面 但是此时可能由于网络问题 并不一定页面详情能够加载出来 所以这里应该要有一个通信的机制
                if (TaskItem.noSee.equals(taskItem.redPoint)) {
                    holder.redMsg.setVisibility(View.GONE);
                }
                TaskDetialActivity.start(context,taskItem.taskId);
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.circleImageView)
        CircleImageView circleImageView;
        @BindView(R.id.taskname)
        TextView taskname;
        @BindView(R.id.totalProgress)
        View totalProgress;
        @BindView(R.id.realProgress)
        View realProgress;
        @BindView(R.id.red_msg)
        View redMsg;
        @BindView(R.id.join_person)
        TextView joinPerson;
        @BindView(R.id.task_finished)
        TextView taskFinished;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    private String nullStr(String str) {
        return TextUtils.isEmpty(str) ? "0" : str;
    }
}
